package com.firstgroup.repository;

import com.firstgroup.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface AdminRepository extends JpaRepository<User,Integer>, JpaSpecificationExecutor<User> {

    //初始化密码
    @Transactional
    @Query(value = "update User set password ='123456' where id = ?1")
    @Modifying
    int updatePassword(Integer userId);

    //初始化所有密码
    @Transactional
    @Query(value = "update User set password ='123456'")
    @Modifying
    int updateAllPassword();

    //模糊查询 根据名称
    @Query(value = "select q from User q where q.name like %?1% ")
    List<User> findByNameLike(String name);

    //模糊查询 根据用户名
    @Query(value = "select q from User q where q.username like %?1% ")
    List<User> findByUsernameLike(String username);

    //根据名称进行查询
    @Query(value = "select u from User u where u.username = ?1 ")
    User findByUserName(String username);

    @Query(value = "select * from user limit ?1,?2 ",nativeQuery = true)
    List<User> findByUser(int start,int pageSize);


}
